﻿using HalconDotNet;
using ImageDefine;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using static DevExpress.XtraPrinting.Native.ExportOptionsPropertiesNames;

namespace GluePathModule
{
    public partial class DrawerControl : UserControl
    {
        public delegate bool ZoomItemAction();
        public event ZoomItemAction zoomItemAction;


        public Matrix MatrixPart;
        public Matrix MatrixPartInvert;

        public Graphics gp;
        Bitmap m_BmpDraw;

        Graphics m_bmpGraphic;

        float fXDis;
        float fYDis;

        bool bInit = false;


        public GluePathItemLine lineCurrent;
        public GluePathItemCircle circleCurrent;
        public GluePathItemArc arcCurrent;
        public GluePathItemPoint pointCurrent;
        public GluePathItemSPLine splineCurrent;

        public GluePathUnit pathUnit;

        public GluePathGroup gluePathGroup;


        public delegate bool CustomMouseDownClick(object sender, MouseEventArgs e);
        public event CustomMouseDownClick customMouseDownClick;

        public delegate bool CustomMouseUpClick(object sender, MouseEventArgs e);
        public event CustomMouseUpClick customMouseUpClick;


      

        float fScan;
        float fTransX;
        float fTransY;


        float fMouseDnPosX;
        float fMouseDnPosY;

        float fMouseUpPosX;
        float fMouseUpPosY;

        public DevExpress.XtraVerticalGrid.PropertyGridControl propertyGrid;

        public BitMapItem bitmapBackGroup;


        public DrawerControl()
        {
            InitializeComponent();
        }
        public void Init(GluePathGroup gluePathGroupInput,GluePathUnit gluePathUnitInput, DevExpress.XtraVerticalGrid.PropertyGridControl propertyGridInput)
        {
            gluePathGroup = gluePathGroupInput;
            pathUnit = gluePathUnitInput;
            propertyGrid = propertyGridInput;
            if (ClientRectangle.Width < 2 || ClientRectangle.Height < 2)
                return;

            fXDis = 200.0f;
            fYDis = 200.0f;
            float resultSize = 0.0f;
            if (fXDis > fYDis)
                resultSize = fXDis;
            else
                resultSize = fYDis;

            float resultPxiSize = 0.0f;
            if (ClientRectangle.Width > ClientRectangle.Height)
                resultPxiSize = ClientRectangle.Height;
            else
                resultPxiSize = ClientRectangle.Width;

            //float fScan = resultPxiSize / resultSize;
            fScan = 1;
            fTransX = 0;
            fTransY = 0;
            // 把原点移到左下角，并把矩形0，0，视野中心
            MatrixPart = new Matrix(1, 0, 0, -1, (ClientRectangle.Width / 2), (ClientRectangle.Height / 2));
            MatrixPart.Scale(fScan, fScan, MatrixOrder.Prepend);
            MatrixPart.Translate(fTransX, fTransY);

            MatrixPartInvert = new Matrix();
            MatrixPartInvert = MatrixPart.Clone();
            MatrixPartInvert.Invert();

            m_BmpDraw = new Bitmap(ClientRectangle.Width, ClientRectangle.Height, System.Drawing.Imaging.PixelFormat.Format24bppRgb);
            m_bmpGraphic = Graphics.FromImage(m_BmpDraw);
            m_bmpGraphic.SmoothingMode = SmoothingMode.AntiAlias;

            bInit = true;
          
            toolStripComboBoxDrawType.SelectedIndex = 0;

            this.MouseWheel += DrawerControl_MouseWheel;
        }

        private void DrawerControl_MouseWheel(object sender, MouseEventArgs e)
        {
            if (e.Delta > 0)
            {
                ZoomItem(true, e.X, e.Y);

            }
            else
            {
                ZoomItem(false, e.X, e.Y);
            }

            if (zoomItemAction != null)
            {
                zoomItemAction();
            }
        }

        public void ZoomItem(bool bUp, int iMouseX, int iMouseY)
        {
            float fscnBefore = fScan;
            float fTransXBefore = fTransX;
            float fTransYBefore = fTransY;

            float fscnNew = fScan;
            float fTransXNew = fTransX;
            float fTransYNew = fTransY;

            if (bUp)
            {
                if (fscnNew < 100)
                {
                    fscnNew = fscnNew * 1.05f;
                }
                else
                {
                    return;
                }
            }
            else
            {
                if (fscnNew > 1)
                {
                    fscnNew = fscnNew * 0.95f;
                }
                else if (fscnNew > 0.6)
                {
                    fscnNew = fscnNew - 0.1f;
                }
                else
                {
                    return;
                }
            }
            float bBeforeMousePosX; float bBeforeMousePosY;
            GetPos(iMouseX, iMouseY, out bBeforeMousePosX, out bBeforeMousePosY);


            Matrix MatrixPartNew = new Matrix(1, 0, 0, -1, (ClientRectangle.Width / 2), (ClientRectangle.Height / 2));
            MatrixPartNew.Scale(fscnNew, fscnNew, MatrixOrder.Prepend);
            MatrixPartNew.Translate(fTransXBefore, fTransYBefore);

            Matrix MatrixPartInvertNew = new Matrix();
            MatrixPartInvertNew = MatrixPartNew.Clone();
            MatrixPartInvertNew.Invert();


            float xPos = 0.0f;
            float yPos = 0.0f;
            PointF[] PtTemp = new System.Drawing.PointF[1];
            PtTemp[0].X = iMouseX;
            PtTemp[0].Y = iMouseY;

            MatrixPartInvertNew.TransformPoints(PtTemp);

            xPos = PtTemp[0].X;
            yPos = PtTemp[0].Y;

            float offsetX = xPos - bBeforeMousePosX;
            float offsetY = yPos - bBeforeMousePosY;

            fTransX = fTransXNew + offsetX;
            fTransY = fTransYNew + offsetY;
            fScan = fscnNew;




            // 把原点移到左下角，并把矩形0，0，视野中心
            MatrixPart = new Matrix(1, 0, 0, -1, (ClientRectangle.Width / 2), (ClientRectangle.Height / 2));
            MatrixPart.Scale(fScan, fScan, MatrixOrder.Prepend);
            MatrixPart.Translate(fTransX, fTransY);

            MatrixPartInvert = new Matrix();
            MatrixPartInvert = MatrixPart.Clone();
            MatrixPartInvert.Invert();

            DrawPathGlueGroup();

            this.Parent.Update();
        }
        private void DrawerControl_Paint(object sender, PaintEventArgs e)
        {
            if (bInit)
            {
                //DrawALL();
                refresh();
            }


        }

        public void refresh()
        {
            Action action = () =>
            {
                gp = Graphics.FromHwnd(this.Handle);
                gp.DrawImage(m_BmpDraw, 0, 0);
            };
            this.Invoke(action);
        }
        public void ClearBackGround()
        {
            m_bmpGraphic.Clear(Color.Black);
        }
        public void DrawOrg()
        {
            //m_bmpGraphic.Clear(Color.Black);
            PointF[] PtTemp = new System.Drawing.PointF[4];
            PtTemp[0].X = (float)0;
            PtTemp[0].Y = (float)0 - 10;
            PtTemp[1].X = (float)0;
            PtTemp[1].Y = (float)10;
            PtTemp[2].X = (float)-10;
            PtTemp[2].Y = (float)0;
            PtTemp[3].X = (float)10;
            PtTemp[3].Y = (float)0;
            MatrixPart.TransformPoints(PtTemp);

            m_bmpGraphic.DrawLine(new Pen(Color.Red, 3), PtTemp[0].X, PtTemp[0].Y, PtTemp[1].X, PtTemp[1].Y);
            m_bmpGraphic.DrawLine(new Pen(Color.Red, 3), PtTemp[2].X, PtTemp[2].Y, PtTemp[3].X, PtTemp[3].Y);


        }

        public void DrawLine(Pen pen, double dPos1X, double dPos1Y, double dPos2X, double dPos2Y)
        {
            PointF[] PtTemp = new System.Drawing.PointF[2];
            PtTemp[0].X = (float)dPos1X;
            PtTemp[0].Y = (float)dPos1Y;
            PtTemp[1].X = (float)dPos2X;
            PtTemp[1].Y = (float)dPos2Y;

            MatrixPart.TransformPoints(PtTemp);
            m_bmpGraphic.DrawLine(pen, PtTemp[0].X, PtTemp[0].Y, PtTemp[1].X, PtTemp[1].Y);



        }
        public void DrawSPLine(Pen pen, List<double> listPosX, List<double> listPosY)
        {
            PointF[] PtTemp = new System.Drawing.PointF[listPosX.Count];
            for (int i = 0; i < listPosX.Count; i++)
            {
                PtTemp[i].X = (float)listPosX[i];
                PtTemp[i].Y = (float)listPosY[i];
            }
            MatrixPart.TransformPoints(PtTemp);
            m_bmpGraphic.DrawLines(pen, PtTemp);

        }
        private void ShowMousePos(float xPos, float yPos)
        {
            PointF[] PtTemp = new System.Drawing.PointF[1];
            PtTemp[0].X = xPos;
            PtTemp[0].Y = yPos;

            MatrixPartInvert.TransformPoints(PtTemp);

            toolStripStatusLabelXPos.Text = PtTemp[0].X.ToString("0.000");
            toolStripStatusLabelYPos.Text = PtTemp[0].Y.ToString("0.000");
        }

        public void DrawCircle(float xPos, float yPos, float dR)
        {
            //m_bmpGraphic.Clear(Color.Black);
            PointF[] PtTemp = new System.Drawing.PointF[3];
            PtTemp[0].X = (float)(xPos - dR);
            PtTemp[0].Y = (float)(yPos + dR);
            PtTemp[1].X = (float)0;
            PtTemp[1].Y = (float)0;
            PtTemp[2].X = (float)2 * dR;
            PtTemp[2].Y = (float)2 * dR;

            MatrixPart.TransformPoints(PtTemp);

            float fWidthX = Math.Abs(PtTemp[2].X - PtTemp[1].X);
            float fWidthY = Math.Abs(PtTemp[2].Y - PtTemp[1].Y);

            m_bmpGraphic.DrawEllipse(new Pen(Color.Green, 3), PtTemp[0].X, PtTemp[0].Y, fWidthX, fWidthY);

        }
        public void DrawCircle(Pen pen, double xCenter, double yCenter, double dR)
        {
            //m_bmpGraphic.Clear(Color.Black);
            PointF[] PtTemp = new System.Drawing.PointF[3];
            PtTemp[0].X = (float)(xCenter - dR);
            PtTemp[0].Y = (float)(yCenter + dR);
            PtTemp[1].X = (float)0;
            PtTemp[1].Y = (float)0;
            PtTemp[2].X = (float)2 * (float)dR;
            PtTemp[2].Y = (float)2 * (float)dR;

            MatrixPart.TransformPoints(PtTemp);

            float fWidthX = Math.Abs(PtTemp[2].X - PtTemp[1].X);
            float fWidthY = Math.Abs(PtTemp[2].Y - PtTemp[1].Y);

            m_bmpGraphic.DrawEllipse(pen, PtTemp[0].X, PtTemp[0].Y, fWidthX, fWidthY);

        }
        public void DrawArc(float xPos, float yPos, float dR, float startAngle, float dSweepAngle)
        {
            //m_bmpGraphic.Clear(Color.Black);
            PointF[] PtTemp = new System.Drawing.PointF[3];
            PtTemp[0].X = (float)(xPos - dR);
            PtTemp[0].Y = (float)(yPos + dR);
            PtTemp[1].X = (float)0;
            PtTemp[1].Y = (float)0;
            PtTemp[2].X = (float)2 * dR;
            PtTemp[2].Y = (float)2 * dR;

            MatrixPart.TransformPoints(PtTemp);

            float fWidthX = Math.Abs(PtTemp[2].X - PtTemp[1].X);
            float fWidthY = Math.Abs(PtTemp[2].Y - PtTemp[1].Y);

            m_bmpGraphic.DrawArc(new Pen(Color.Green, 3), PtTemp[0].X, PtTemp[0].Y, fWidthX, fWidthY, -startAngle, -dSweepAngle);

        }
        public void DrawArc(float xPos1, float yPos1, float xPos2, float yPos2, float xPos3, float yPos3)
        {
            double xCenterPos; double yCenterPos; double dR;
            //m_bmpGraphic.Clear(Color.Black);
            if (GetCircle(xPos1, yPos1, xPos2, yPos2, xPos3, yPos3, out xCenterPos, out yCenterPos, out dR) == false)
                return;
            double dStartAnglePi = Math.Atan2(yPos1 - yCenterPos, xPos1 - xCenterPos);
            double dStartAngle = 180 * dStartAnglePi / Math.PI;
            double dEndAnglePi = Math.Atan2(yPos3 - yCenterPos, xPos3 - xCenterPos);
            double dEndAngle = 180 * dEndAnglePi / Math.PI;
            double dSweeAngle = dEndAngle - dStartAngle;
            DrawArc((float)xCenterPos, (float)yCenterPos, (float)dR, (float)dStartAngle, (float)dSweeAngle);

        }
        public void DrawArc(Pen pen, double xPos1, double yPos1, double xPos2, double yPos2, double xPos3, double yPos3)
        {
            double xCenterPos; double yCenterPos; double dR;
            //m_bmpGraphic.Clear(Color.Black);
            if (GetCircle(xPos1, yPos1, xPos2, yPos2, xPos3, yPos3, out xCenterPos, out yCenterPos, out dR) == false)
                return;
            double dStartAnglePi = Math.Atan2(yPos1 - yCenterPos, xPos1 - xCenterPos);
            double dStartAngle = 180 * dStartAnglePi / Math.PI;
            double dMidAnglePi = Math.Atan2(yPos2 - yCenterPos, xPos2 - xCenterPos);
            double dMidAngle = 180 * dMidAnglePi / Math.PI;
            double dEndAnglePi = Math.Atan2(yPos3 - yCenterPos, xPos3 - xCenterPos);
            double dEndAngle = 180 * dEndAnglePi / Math.PI;
            double dSweeAngle;
            dSweeAngle = dEndAngle - dStartAngle;
            if (dSweeAngle > 0)
            {
                if (dMidAngle > dStartAngle && dMidAngle < dEndAngle)
                {
                    dSweeAngle = dEndAngle - dStartAngle;
                }
                else
                {
                    dSweeAngle = -(360 - (dEndAngle - dStartAngle));
                }
            }
            else
            {
                if (dMidAngle < dStartAngle && dMidAngle > dEndAngle)
                {
                    dSweeAngle = dEndAngle - dStartAngle;
                }
                else
                {
                    dSweeAngle = (360 - (dStartAngle - dEndAngle));
                }
            }

            //dSweeAngle = dEndAngle - dStartAngle;

            //dSweeAngle = 360 - dSweeAngle;
            DrawArc(pen, (float)xCenterPos, (float)yCenterPos, (float)dR, (float)dStartAngle, (float)dSweeAngle);

        }
        public void DrawArc(Pen pen, float xCenter, float yCenter, float dR, float startAngle, float dSweepAngle)
        {
            //m_bmpGraphic.Clear(Color.Black);
            PointF[] PtTemp = new System.Drawing.PointF[3];
            PtTemp[0].X = (float)(xCenter - dR);
            PtTemp[0].Y = (float)(yCenter + dR);
            PtTemp[1].X = (float)0;
            PtTemp[1].Y = (float)0;
            PtTemp[2].X = (float)2 * dR;
            PtTemp[2].Y = (float)2 * dR;

            MatrixPart.TransformPoints(PtTemp);

            float fWidthX = Math.Abs(PtTemp[2].X - PtTemp[1].X);
            float fWidthY = Math.Abs(PtTemp[2].Y - PtTemp[1].Y);

            m_bmpGraphic.DrawArc(pen, PtTemp[0].X, PtTemp[0].Y, fWidthX, fWidthY, -startAngle, -dSweepAngle);

        }
        public void DrawBitmap()
        {
            if (bitmapBackGroup == null)
            {
                double dStartPosX, dStartPosY, dEndPosX, dEndPosY;
                bitmapBackGroup =new BitMapItem();
                bitmapBackGroup.bitmap=GetWorldMapImage(out dStartPosX,out dStartPosY,out dEndPosX,out dEndPosY);
                bitmapBackGroup.dStartPosX = dStartPosX;
                bitmapBackGroup.dStartPosY = dStartPosY;
                bitmapBackGroup.dEndX = dEndPosX;
                bitmapBackGroup.dEndY = dEndPosY;
            }
            if(bitmapBackGroup.bitmap!=null)
            {
                DrawBitmap(bitmapBackGroup.bitmap, bitmapBackGroup.dStartPosX, bitmapBackGroup.dStartPosY,
                        bitmapBackGroup.dEndX, bitmapBackGroup.dEndY);
            }       
            
               

           
        }
        public void DrawBitmap(Bitmap bitmap, double dStartPosX, double dStartPosY, double dEndPosX, double dEndPosY)
        {
            PointF[] PtTemp = new System.Drawing.PointF[2];
            PtTemp[0].X = (float)dStartPosX;
            PtTemp[0].Y = (float)dStartPosY;
            PtTemp[1].X = (float)dEndPosX;
            PtTemp[1].Y = (float)dEndPosY;
            MatrixPart.TransformPoints(PtTemp);
            m_bmpGraphic.DrawImage(bitmap, new Rectangle((int)(PtTemp[0].X), (int)PtTemp[0].Y, (int)PtTemp[1].X - (int)PtTemp[0].X, (int)PtTemp[1].Y - (int)PtTemp[0].Y),
                0, 0, bitmap.Width, bitmap.Height, GraphicsUnit.Pixel);
            
        }
        public bool GetCircle(double dX1, double dY1, double dX2, double dY2, double dX3, double dY3,
           out double dCenterX, out double dCenterY, out double dR)
        {
            dCenterX = 0.0;
            dCenterY = 0.0;
            dR = 0.0;
            double dA1, dB1, dA2, dB2;
            double dCrossX = 0.0;
            double dCrossY = 0.0;
            //Line1
            bool bLine1Ok = GetLine(dX1, dY1, dX2, dY2, out dA1, out dB1);

            //Line1
            bool bLine2Ok = GetLine(dX2, dY2, dX3, dY3, out dA2, out dB2);
            //Cross

            if (bLine1Ok && bLine2Ok)
            {
                LineCross(dA1, dB1, dA2, dB2, out dCrossX, out dCrossY);
            }
            else
            {
                if (bLine1Ok == false && bLine2Ok == false)
                {
                    return false;
                }
                if (bLine1Ok == false)
                {
                    dCrossX = dB1;
                    dCenterY = dCrossX * dA2 + dB2;
                }
                if (bLine2Ok == false)
                {
                    dCrossX = dB2;
                    dCenterY = dCrossX * dA1 + dB1;
                }
            }
            dCenterX = dCrossX;
            dCenterY = dCrossY;
            dR = Math.Sqrt((dX1 - dCrossX) * (dX1 - dCrossX) + (dY1 - dCrossY) * (dY1 - dCrossY));
            return true;
        }
        private bool GetLine(double dX1, double dY1, double dX2, double dY2, out double dA, out double dB)
        {
            dA = 0.0;
            dB = 0.0;
            double dTempA, dTempB;
            double dX, dY;
            if (Math.Abs(dX1 - dX2) < 0.00000001)
            {
                dA = 0.0;
                dB = (dY1 + dY2) / 2.0;
                return true;
            }
            if (Math.Abs(dY1 - dY2) < 0.00000001)
            {
                dA = 0.0;
                dB = (dX1 + dX2) / 2.0;
                return false;
            }
            dTempA = (dY2 - dY1) / (dX2 - dX1);
            dTempB = dY1 - dA * dX1;
            //计算直线中点
            dX = (dX1 + dX2) / 2.0;
            dY = (dY1 + dY2) / 2.0;
            //中垂线
            dA = -1 / dTempA;
            dB = dY - dA * dX;
            return true;

        }
        private void LineCross(double dA1, double dB1, double dA2, double dB2, out double dCrossX, out double dCrossY)
        {
            dCrossX = 0.0;
            dCrossY = 0.0;
            dCrossX = (dB1 - dB2) / (dA2 - dA1);
            dCrossY = dA1 * dCrossX + dB1;
        }

        private void toolStripButtonSelect_Click(object sender, EventArgs e)
        {
            this.Cursor = Cursors.Arrow;
            SetUnCheckAll();
            toolStripButtonSelect.Checked = true;
        }

        private void toolStripButtonLine_Click(object sender, EventArgs e)
        {
            SetUnCheckAll();
            this.Cursor = Cursors.Cross;
            toolStripButtonLine.Checked = true;
            if (toolStripButtonContinue.Checked)
            {
                if (pathUnit.listContinueItems.Count > 0)
                {
                    
                    if (toolStripComboBoxDrawType.SelectedIndex == 0)
                    {
                        lineCurrent = new GluePathItemLine();
                        double dLastPosX, dLastPosY, dLastPosZ, dLastPosU;
                        int iIndex = pathUnit.listContinueItems.Count - 1;
                        pathUnit.listContinueItems[iIndex].GetLastPoint(out dLastPosX, out dLastPosY,out dLastPosZ,out dLastPosU);
                        lineCurrent.Pos1.X = dLastPosX;
                        lineCurrent.Pos1.Y = dLastPosY;
                        lineCurrent.iDrawStep = 1;
                    }
                    else
                    {
                        lineCurrent = null;
                    }
                }
                else
                {
                    lineCurrent = null;
                }
            }
            else
            {
                lineCurrent = null;
            }
        }
        private void SetUnCheckAll()
        {
            toolStripButtonSelect.Checked = false;
            toolStripButtonLine.Checked = false;
            toolStripButtonCircle.Checked = false;
            toolStripButtonArc.Checked = false;
            toolStripButtonPoint.Checked = false;
            toolStripButtonModify.Checked = false;
            toolStripButtonSPLine.Checked = false;
        }
        private void DrawerControl_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Right)
            {
                if (splineCurrent != null)
                {
                    splineCurrent.ListPoints.RemoveAt(splineCurrent.ListPoints.Count - 1);
                    pathUnit.listContinueItems.Add(splineCurrent);
                    splineCurrent.iDrawStep = 0;
                    DrawPathGlueGroup();

                }
                lineCurrent = null;
                circleCurrent = null;
                arcCurrent = null;
                pointCurrent = null;
                splineCurrent = null;


                UpdateSelect(99999.9, 99999.9);
                SetUnCheckAll();
                toolStripButtonSelect.Checked = true;
                DrawPathGlueGroup();
                if (customMouseDownClick != null)
                {
                    customMouseDownClick(sender, e);
                }
                //toolStripButtonSPLine
                return;
            }
            if (toolStripButtonSelect.Checked)
            {

                float dX; float dY;
                GetPos(e.X, e.Y, out dX, out dY);

                fMouseDnPosX = dX;
                fMouseDnPosY = dY;
                //PathUnitMouseDown(dX, dY);
                UpdateSelect(dX, dY);
                //PathUnitMouseDown(dX, dY);
                DrawPathGlueGroup();
            }
            else if (toolStripButtonModify.Checked)
            {

                float dX; float dY;
                GetPos(e.X, e.Y, out dX, out dY);
                PathUnitMouseDown(dX, dY);
                //UpdateSelect(dX, dY);
                DrawPathGlueGroup();
            }
            else if (toolStripButtonLine.Checked)
            {
                if (lineCurrent == null)
                {
                    lineCurrent = new GluePathItemLine();
                }
            }
            else if (toolStripButtonCircle.Checked)
            {
                if (circleCurrent == null)
                {
                    circleCurrent = new GluePathItemCircle();
                }
            }
            else if (toolStripButtonArc.Checked)
            {
                if (arcCurrent == null)
                {
                    arcCurrent = new GluePathItemArc();
                }
            }
            else if (toolStripButtonPoint.Checked)
            {
                if (pointCurrent == null)
                {

                    pointCurrent = new GluePathItemPoint();
                    pointCurrent.Select = true;
                }
            }
            else if (toolStripButtonSPLine.Checked)
            {
                if (splineCurrent == null)
                {

                    splineCurrent = new GluePathItemSPLine();
                    splineCurrent.Select = true;
                    float dX; float dY;
                    GetPos(e.X, e.Y, out dX, out dY);
                    PointData newPoint = new PointData();
                    newPoint.X = dX;
                    newPoint.Y = dY;
                    splineCurrent.ListPoints.Add(newPoint);
                    PointData newPoint1 = new PointData();
                    newPoint1.X = dX;
                    newPoint1.Y = dY;
                    splineCurrent.ListPoints.Add(newPoint1);
                    DrawPathGlueGroup();
                }
                else
                {
                    DrawPathGlueGroup();
                    float dX; float dY;
                    GetPos(e.X, e.Y, out dX, out dY);
                    splineCurrent.ListPoints[splineCurrent.ListPoints.Count - 1].X = dX;
                    splineCurrent.ListPoints[splineCurrent.ListPoints.Count - 1].Y = dY;
                    PointData newPoint = new PointData();
                    newPoint.X = dX;
                    newPoint.Y = dY;
                    splineCurrent.ListPoints.Add(newPoint);

                    splineCurrent.DrawItem(this);
                    refresh();

                }
            }

            if (customMouseDownClick != null)
            {
                customMouseDownClick(sender, e);
            }
        }
        private void DrawerControl_MouseMove(object sender, MouseEventArgs e)
        {
            if (bInit == false)
                return;
            if (toolStripButtonLine.Checked)
            {
                if (lineCurrent != null)
                {
                    if (lineCurrent.iDrawStep == 1)
                    {
                        DrawPathGlueGroup();
                        float dX; float dY;
                        GetPos(e.X, e.Y, out dX, out dY);
                        lineCurrent.Pos2.X = dX;
                        lineCurrent.Pos2.Y = dY;
                        lineCurrent.Select = true;
                        lineCurrent.DrawItem(this);
                        refresh();
                    }
                }

            }
            else if (toolStripButtonCircle.Checked)
            {
                if (circleCurrent != null)
                {
                    if (circleCurrent.iDrawStep == 1)
                    {
                        DrawPathGlueGroup();
                        float dX; float dY;
                        GetPos(e.X, e.Y, out dX, out dY);
                        circleCurrent.Center.X = dX;
                        circleCurrent.Center.Y = dY;
                        double dR = Math.Sqrt((circleCurrent.Start.X - circleCurrent.Center.X) * (circleCurrent.Start.X - circleCurrent.Center.X) + (circleCurrent.Start.Y - circleCurrent.Center.Y) * (circleCurrent.Start.Y - circleCurrent.Center.Y));
                        circleCurrent.R = dR;
                        circleCurrent.Select = true;
                        circleCurrent.DrawItem(this);
                        //DrawCircle(new Pen(Color.White, 3), circleCurrent.CenterX, circleCurrent.CenterY, circleCurrent.R);
                        refresh();
                    }
                }
            }
            else if (toolStripButtonArc.Checked)
            {
                if (arcCurrent != null)
                {
                    if (arcCurrent.iDrawStep == 1)
                    {
                        DrawPathGlueGroup();
                        float dX; float dY;
                        GetPos(e.X, e.Y, out dX, out dY);
                        arcCurrent.PosEnd.X = dX;
                        arcCurrent.PosEnd.Y = dY;

                        DrawLine(new Pen(Color.Red, 3), arcCurrent.PosStart.X, arcCurrent.PosStart.Y, arcCurrent.PosEnd.X, arcCurrent.PosEnd.Y);
                        refresh();
                    }
                    if (arcCurrent.iDrawStep == 2)
                    {
                        DrawPathGlueGroup();
                        float dX; float dY;
                        GetPos(e.X, e.Y, out dX, out dY);
                        arcCurrent.PosMid.X = dX;
                        arcCurrent.PosMid.Y = dY;
                        arcCurrent.Select = true;
                        arcCurrent.DrawItem(this);
                        refresh();
                    }
                }
            }
            else if (toolStripButtonPoint.Checked)
            {
                if (pointCurrent != null)
                {

                    DrawPathGlueGroup();
                    float dX; float dY;
                    GetPos(e.X, e.Y, out dX, out dY);
                    pointCurrent.Pos.X = dX;
                    pointCurrent.Pos.Y = dY;

                    pointCurrent.Select = true;
                    pointCurrent.DrawItem(this);
                    refresh();

                }
            }
            else if (toolStripButtonSelect.Checked)
            {
                float dX; float dY;
                GetPos(e.X, e.Y, out dX, out dY);
                //PathUnitMouseMove(dX, dY);
                //if (GetCurrentUnitHaveSelectItem())
                //{
                //    DrawPathGlueGroup();
                //}
            }
            else if (toolStripButtonModify.Checked)
            {
                float dX; float dY;
                GetPos(e.X, e.Y, out dX, out dY);
                PathUnitMouseMove(dX, dY);
                DrawPathGlueGroup();
            }
            else if (toolStripButtonSPLine.Checked)
            {
                if (splineCurrent != null)
                {
                    DrawPathGlueGroup();
                    float dX; float dY;
                    GetPos(e.X, e.Y, out dX, out dY);
                    splineCurrent.ListPoints[splineCurrent.ListPoints.Count - 1].X = dX;
                    splineCurrent.ListPoints[splineCurrent.ListPoints.Count - 1].Y = dY;

                    splineCurrent.DrawItem(this);
                    refresh();
                }
            }
            ShowMousePos(e.X, e.Y);
        }
        private void DrawerControl_MouseUp(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Right)
            {
                lineCurrent = null;
                circleCurrent = null;
                arcCurrent = null;
                pointCurrent = null;
                foreach (GluePathItemBase item in pathUnit.listContinueItems)
                {
                    item.Select = false;
                    item.iAdjustStep = 0;
                }
                DrawPathGlueGroup();
                if (customMouseUpClick != null)
                {
                    customMouseUpClick(sender, e);
                }
                this.Cursor = Cursors.Arrow;
                SetUnCheckAll();
                toolStripButtonSelect.Checked = true;
                propertyGrid.SelectedObject = null;
                return;
            }
            if (toolStripButtonSelect.Checked)
            {
                float dX; float dY;
                GetPos(e.X, e.Y, out dX, out dY);
                fMouseUpPosX = dX;
                fMouseUpPosY = dY;
                if (GetCurrentUnitHaveSelectItem() == false)
                {
                    if (Math.Abs(fMouseUpPosX - fMouseDnPosX) > 1 || Math.Abs(fMouseUpPosY - fMouseDnPosY) > 1)
                    {
                        fTransX = fTransX + fMouseUpPosX - fMouseDnPosX;
                        fTransY = fTransY + fMouseUpPosY - fMouseDnPosY;
                        MatrixPart = new Matrix(1, 0, 0, -1, (ClientRectangle.Width / 2), (ClientRectangle.Height / 2));
                        MatrixPart.Scale(fScan, fScan, MatrixOrder.Prepend);
                        MatrixPart.Translate(fTransX, fTransY);

                        MatrixPartInvert = new Matrix();
                        MatrixPartInvert = MatrixPart.Clone();
                        MatrixPartInvert.Invert();

                    }
                }
                //PathUnitMouseUp(dX, dY);
                DrawPathGlueGroup();
            }
            else if (toolStripButtonModify.Checked)
            {
                float dX; float dY;
                GetPos(e.X, e.Y, out dX, out dY);
                PathUnitMouseUp(dX, dY);
                DrawPathGlueGroup();
            }
            else if (toolStripButtonLine.Checked)
            {
                if (lineCurrent == null)
                {
                    return;
                }
                if (lineCurrent.iDrawStep == 0)
                {
                    float dX; float dY;
                    GetPos(e.X, e.Y, out dX, out dY);
                    lineCurrent.Pos1.X = dX;
                    lineCurrent.Pos1.Y = dY;

                    lineCurrent.iDrawStep = 1;

                }
                else if (lineCurrent.iDrawStep == 1)
                {
                    if (toolStripComboBoxDrawType.SelectedIndex == 0)
                    {
                        float dX; float dY;
                        GetPos(e.X, e.Y, out dX, out dY);
                        lineCurrent.Pos2.X = dX;
                        lineCurrent.Pos2.Y = dY;
                        pathUnit.listContinueItems.Add(lineCurrent);
                        lineCurrent.iDrawStep = 0;
                        DrawPathGlueGroup();
                        if (toolStripButtonContinue.Checked)
                        {
                            lineCurrent = new GluePathItemLine();
                            double dLastPosX, dLastPosY, dLastPosZ, dLastPosU;
                            int iIndex = pathUnit.listContinueItems.Count - 1;
                            pathUnit.listContinueItems[iIndex].GetLastPoint(out dLastPosX, out dLastPosY,out dLastPosZ,out dLastPosU);
                            lineCurrent.Pos1.X = dLastPosX;
                            lineCurrent.Pos1.Y = dLastPosY;
                            lineCurrent.iDrawStep = 1;
                        }
                        else
                        {
                            lineCurrent = null;
                        }
                    }
                    else
                    {
                        float dX; float dY;
                        GetPos(e.X, e.Y, out dX, out dY);
                        lineCurrent.Pos2.X = dX;
                        lineCurrent.Pos2.Y = dY;
                        int iFirstSelectIndex = GetFirstSelectItemIndex();
                        if (iFirstSelectIndex > -1)
                        {
                            pathUnit.listContinueItems.Insert(iFirstSelectIndex, lineCurrent);
                            lineCurrent.iDrawStep = 0;
                            DrawPathGlueGroup();
                            lineCurrent = null;
                        }
                        else
                        {
                            DrawPathGlueGroup();
                            lineCurrent = null;
                            MessageBox.Show("在插入模式下没有选择对象");
                        }

                    }

                }
            }
            else if (toolStripButtonCircle.Checked)
            {
                if (circleCurrent == null)
                {
                    return;
                }
                if (circleCurrent.iDrawStep == 0)
                {
                    float dX; float dY;
                    GetPos(e.X, e.Y, out dX, out dY);
                    circleCurrent.Start.X = dX;
                    circleCurrent.Start.Y = dY;
                    circleCurrent.iDrawStep = 1;

                }
                else if (circleCurrent.iDrawStep == 1)
                {
                    if (toolStripComboBoxDrawType.SelectedIndex == 0)
                    {
                        float dX; float dY;
                        GetPos(e.X, e.Y, out dX, out dY);
                        circleCurrent.Center.X = dX;
                        circleCurrent.Center.Y = dY;
                        double dR = Math.Sqrt((circleCurrent.Start.X - circleCurrent.Center.X) * (circleCurrent.Start.X - circleCurrent.Center.X) + (circleCurrent.Start.Y - circleCurrent.Center.Y) * (circleCurrent.Start.Y - circleCurrent.Center.Y));
                        circleCurrent.R = dR;
                        pathUnit.listContinueItems.Add(circleCurrent);
                        circleCurrent.iDrawStep = 0;
                        DrawPathGlueGroup();

                        if (toolStripButtonContinue.Checked)
                        {

                            circleCurrent = new GluePathItemCircle();
                            double dLastPosX, dLastPosY, dLastPosZ, dLastPosU;
                            int iIndex = pathUnit.listContinueItems.Count - 1;
                            pathUnit.listContinueItems[iIndex].GetLastPoint(out dLastPosX, out dLastPosY, out dLastPosZ, out dLastPosU);
                            circleCurrent.Start.X = dLastPosX;
                            circleCurrent.Start.Y = dLastPosY;
                            circleCurrent.iDrawStep = 1;
                        }
                        else
                        {
                            circleCurrent = null;
                        }
                    }
                    else
                    {
                        float dX; float dY;
                        GetPos(e.X, e.Y, out dX, out dY);
                        circleCurrent.Center.X = dX;
                        circleCurrent.Center.Y = dY;
                        double dR = Math.Sqrt((circleCurrent.Start.X - circleCurrent.Center.X) * (circleCurrent.Start.X - circleCurrent.Center.X) + (circleCurrent.Start.Y - circleCurrent.Center.Y) * (circleCurrent.Start.Y - circleCurrent.Center.Y));
                        circleCurrent.R = dR;
                        int iFirstSelectIndex = GetFirstSelectItemIndex();
                        if (iFirstSelectIndex > -1)
                        {
                            pathUnit.listContinueItems.Insert(iFirstSelectIndex, circleCurrent);
                            circleCurrent.iDrawStep = 0;
                            DrawPathGlueGroup();
                            circleCurrent = null;
                        }
                        else
                        {
                            DrawPathGlueGroup();
                            circleCurrent = null;
                            MessageBox.Show("在插入模式下没有选择对象");
                        }
                    }
                    //circleCurrent = null;
                }
            }
            else if (toolStripButtonArc.Checked)
            {
                if (arcCurrent == null)
                {
                    return;
                }
                if (arcCurrent.iDrawStep == 0)
                {
                    float dX; float dY;
                    GetPos(e.X, e.Y, out dX, out dY);
                    arcCurrent.PosStart.X = dX;
                    arcCurrent.PosStart.Y = dY;
                    arcCurrent.iDrawStep = 1;

                }
                else if (arcCurrent.iDrawStep == 1)
                {
                    DrawPathGlueGroup();
                    float dX; float dY;
                    GetPos(e.X, e.Y, out dX, out dY);
                    arcCurrent.PosEnd.X = dX;
                    arcCurrent.PosEnd.Y = dY;
                    DrawLine(new Pen(Color.White, 3), arcCurrent.PosStart.X, arcCurrent.PosStart.Y, arcCurrent.PosEnd.X, arcCurrent.PosEnd.Y);
                    arcCurrent.iDrawStep = 2;

                }
                else if (arcCurrent.iDrawStep == 2)
                {
                    if (toolStripComboBoxDrawType.SelectedIndex == 0)
                    {
                        DrawPathGlueGroup();
                        float dX; float dY;
                        GetPos(e.X, e.Y, out dX, out dY);
                        arcCurrent.PosMid.X = dX;
                        arcCurrent.PosMid.Y = dY;

                        arcCurrent.DrawItem(this);
                        refresh();

                        pathUnit.listContinueItems.Add(arcCurrent);
                        arcCurrent.iDrawStep = 0;

                        if (toolStripButtonContinue.Checked)
                        {
                            arcCurrent = new GluePathItemArc();
                            double dLastPosX, dLastPosY, dLastPosZ, dLastPosU;
                            int iIndex = pathUnit.listContinueItems.Count - 1;
                            pathUnit.listContinueItems[iIndex].GetLastPoint(out dLastPosX, out dLastPosY, out dLastPosZ, out dLastPosU);
                            arcCurrent.PosStart.X = dLastPosX;
                            arcCurrent.PosStart.Y = dLastPosY;
                            arcCurrent.iDrawStep = 1;
                        }
                        else
                        {
                            arcCurrent = null;
                        }
                    }
                    else
                    {
                        DrawPathGlueGroup();
                        float dX; float dY;
                        GetPos(e.X, e.Y, out dX, out dY);

                        arcCurrent.PosMid.X = dX;
                        arcCurrent.PosMid.Y = dY;

                        arcCurrent.DrawItem(this);
                        
                        refresh();
                        int iFirstSelectIndex = GetFirstSelectItemIndex();
                        if (iFirstSelectIndex > -1)
                        {
                            pathUnit.listContinueItems.Insert(iFirstSelectIndex, arcCurrent);
                            arcCurrent = null;
                        }
                        else
                        {
                            DrawPathGlueGroup();
                            arcCurrent = null;
                            MessageBox.Show("在插入模式下没有选择对象");
                        }
                    }
                }
            }
            else if (toolStripButtonPoint.Checked)
            {
                if (pointCurrent == null)
                {
                    return;
                }
                if (toolStripComboBoxDrawType.SelectedIndex == 0)
                {
                    DrawPathGlueGroup();
                    float dX; float dY;
                    GetPos(e.X, e.Y, out dX, out dY);

                    pointCurrent.Pos.X = dX;
                    pointCurrent.Pos.Y = dY;

                    pointCurrent.DrawItem(this);

                    refresh();

                    pathUnit.listContinueItems.Add(pointCurrent);
                    pointCurrent.iDrawStep = 0;


                    if (toolStripButtonContinue.Checked)
                    {
                        pointCurrent = new GluePathItemPoint();
                        double dLastPosX, dLastPosY, dLastPosZ, dLastPosU;
                        int iIndex = pathUnit.listContinueItems.Count - 1;
                        pathUnit.listContinueItems[iIndex].GetLastPoint(out dLastPosX, out dLastPosY, out dLastPosZ, out dLastPosU);
                        pointCurrent.Pos.X = dLastPosX;
                        pointCurrent.Pos.Y = dLastPosY;
                        pointCurrent.iDrawStep = 0;
                    }
                    else
                    {
                        pointCurrent = null;
                    }
                }
                else
                {
                    DrawPathGlueGroup();
                    float dX; float dY;
                    GetPos(e.X, e.Y, out dX, out dY);
                    pointCurrent.Pos.X = dX;
                    pointCurrent.Pos.Y = dY;

                    pointCurrent.DrawItem(this);

                    refresh();
                    int iFirstSelectIndex = GetFirstSelectItemIndex();
                    if (iFirstSelectIndex > -1)
                    {
                        pathUnit.listContinueItems.Insert(iFirstSelectIndex, pointCurrent);
                        pointCurrent = null;
                    }
                    else
                    {
                        DrawPathGlueGroup();
                        pointCurrent = null;
                        MessageBox.Show("在插入模式下没有选择对象");
                    }
                }

            }
            else if (toolStripButtonSPLine.Checked)
            {
                if (splineCurrent != null)
                {
                    DrawPathGlueGroup();
                    float dX; float dY;
                    GetPos(e.X, e.Y, out dX, out dY);
                    splineCurrent.ListPoints[splineCurrent.ListPoints.Count - 1].X = dX;
                    splineCurrent.ListPoints[splineCurrent.ListPoints.Count - 1].Y = dY;

                    splineCurrent.DrawItem(this);
                    refresh();
                }
            }
            if (customMouseUpClick != null)
            {
                customMouseUpClick(sender, e);
            }
        }
        private void GetPos(float xPosMouse, float yPosMouse, out float xPos, out float yPos)
        {
            xPos = 0.0f;
            yPos = 0.0f;
            PointF[] PtTemp = new System.Drawing.PointF[1];
            PtTemp[0].X = xPosMouse;
            PtTemp[0].Y = yPosMouse;

            MatrixPartInvert.TransformPoints(PtTemp);

            xPos = PtTemp[0].X;
            yPos = PtTemp[0].Y;
        }



        private void toolStripButtonCircle_Click(object sender, EventArgs e)
        {
            SetUnCheckAll();
            this.Cursor = Cursors.Cross;
            toolStripButtonCircle.Checked = true;

            if (toolStripButtonContinue.Checked)
            {
                if (pathUnit.listContinueItems.Count > 0)
                {
                    if (toolStripComboBoxDrawType.SelectedIndex == 0)
                    {
                        circleCurrent = new GluePathItemCircle();
                        double dLastPosX, dLastPosY, dLastPosZ, dLastPosU;
                        int iIndex = pathUnit.listContinueItems.Count - 1;
                        pathUnit.listContinueItems[iIndex].GetLastPoint(out dLastPosX, out dLastPosY, out dLastPosZ, out dLastPosU);
                        circleCurrent.Start.X = dLastPosX;
                        circleCurrent.Start.Y = dLastPosY;
                        circleCurrent.iDrawStep = 1;
                    }
                    else
                    {
                        circleCurrent = null;
                    }
                }
                else
                {
                    circleCurrent = null;
                }
            }
            else
            {
                circleCurrent = null;
            }
        }

        private void toolStripButtonArc_Click(object sender, EventArgs e)
        {
            SetUnCheckAll();
            this.Cursor = Cursors.Cross;
            toolStripButtonArc.Checked = true;
            //arcCurrent = null;

            if (toolStripButtonContinue.Checked)
            {
                if (pathUnit.listContinueItems.Count > 0)
                {
                    if (toolStripComboBoxDrawType.SelectedIndex == 0)
                    {
                        arcCurrent = new GluePathItemArc();
                        double dLastPosX, dLastPosY, dLastPosZ, dLastPosU;
                        int iIndex = pathUnit.listContinueItems.Count - 1;
                        pathUnit.listContinueItems[iIndex].GetLastPoint(out dLastPosX, out dLastPosY, out dLastPosZ, out dLastPosU);
                        arcCurrent.PosStart.X = dLastPosX;
                        arcCurrent.PosStart.Y = dLastPosY;
                        arcCurrent.iDrawStep = 1;
                    }
                    else
                    {
                        arcCurrent = null;
                    }
                }
                else
                {
                    arcCurrent = null;
                }
            }
            else
            {
                arcCurrent = null;
            }
        }
        public void UpdateSelect(double dPosX, double dPosY)
        {

            foreach (GluePathItemBase item in pathUnit.listContinueItems)
            {
                bool bBeforeSelect = item.Select;
                item.IsOnSelect(dPosX, dPosY, (5 / fScan));
                bool bCurrentSelect= item.Select;
                if (propertyGrid != null)
                {
                    if (bBeforeSelect == false & bCurrentSelect == true)
                    {
                        propertyGrid.SelectedObject = item;
                    }
                }
            }

        }
        public void DrawPathGlueGroup()
        {
            ClearBackGround();
            DrawBitmap();
            DrawBackGround();
            DrawOrg();
            foreach (GluePathUnit item in gluePathGroup.listGlueUnit)
            {
                DrawPathGlueUnit(item);
            }
            refresh();
        }
        public void DrawPathGlueUnit(GluePathUnit unitItem)
        {
            DrawLocalOrg(unitItem);
            foreach (GluePathItemBase item in unitItem.listContinueItems)
            {
                item.DrawItem(this, unitItem.drawMode);
            }
        }
        BackgroundDrawer backgroundDrawer = null;
        public void DrawBackGround()
        {
            if (backgroundDrawer == null)
            {
                backgroundDrawer = new BackgroundDrawer(gluePathGroup);
                backgroundDrawer.Init();
            }
            backgroundDrawer.DrawPathUnit(this);

        }
        
        public void DrawLocalOrg(GluePathUnit unitItem)
        {

            double line1X1 = unitItem.StandardLocalX + 10 * Math.Cos((unitItem.StandardLocalU + 180.0) * Math.PI / 180.0);
            double line1Y1 = unitItem.StandardLocalY + 10 * Math.Sin((unitItem.StandardLocalU + 180.0) * Math.PI / 180.0);
            double line1X2 = unitItem.StandardLocalX + 10 * Math.Cos((unitItem.StandardLocalU) * Math.PI / 180.0);
            double line1Y2 = unitItem.StandardLocalY + 10 * Math.Sin((unitItem.StandardLocalU) * Math.PI / 180.0);

            double line2X1 = unitItem.StandardLocalX + 10 * Math.Cos((unitItem.StandardLocalU + 270.0) * Math.PI / 180.0);
            double line2Y1 = unitItem.StandardLocalY + 10 * Math.Sin((unitItem.StandardLocalU + 270.0) * Math.PI / 180.0);
            double line2X2 = unitItem.StandardLocalX + 10 * Math.Cos((unitItem.StandardLocalU + 90.0) * Math.PI / 180.0);
            double line2Y2 = unitItem.StandardLocalY + 10 * Math.Sin((unitItem.StandardLocalU + 90.0) * Math.PI / 180.0);

           
            DrawLine(new Pen(Color.Blue, 3), line1X1, line1Y1, line1X2, line1Y2);
            DrawLine(new Pen(Color.Blue, 3), line2X1, line2Y1, line2X2, line2Y2);
            


        }
        private void toolStripButtonPoint_Click(object sender, EventArgs e)
        {
            SetUnCheckAll();
            this.Cursor = Cursors.Cross;
            toolStripButtonPoint.Checked = true;
            pointCurrent = null;
        }
        private int GetFirstSelectItemIndex()
        {
            int iReturn = -1;
            for (int i = 0; i < pathUnit.listContinueItems.Count; i++)
            {
                if (pathUnit.listContinueItems[i].Select)
                {
                    iReturn = i;
                    break;
                }
            }
            return iReturn;
        }
        public void PathUnitMouseDown(double dMouseX, double dMouseY)
        {
          
            foreach (GluePathItemBase item in pathUnit.listContinueItems)
            {
                item.PathUnitMouseDown(dMouseX, dMouseY, fScan);
            }
        }
        public void PathUnitMouseMove(double dMouseX, double dMouseY)
        {
           
            foreach (GluePathItemBase item in pathUnit.listContinueItems)
            {
                item.PathUnitMouseMove(dMouseX, dMouseY);
            }
        }
        public void PathUnitMouseUp(double dMouseX, double dMouseY)
        {
            
            foreach (GluePathItemBase item in pathUnit.listContinueItems)
            {
                item.PathUnitMouseUp(dMouseX, dMouseY);
            }
        }

        private void toolStripButtonDefault_Click(object sender, EventArgs e)
        {



            SetToAutoFill();


        }
        public void SetToAutoFill()
        {
            if(gluePathGroup==null)
                return;
            double dMaxX, dMaxY, dMinX, dMinY;
            dMaxX = -9999;
            dMaxY = -9999;

            dMinX = 9999;
            dMinY = 9999;
            if (gluePathGroup.listGlueUnit.Count < 1)
                return;
            bool bNeedModify = false;
            for (int i = 0; i < gluePathGroup.listGlueUnit.Count; i++)
            {
                GluePathUnit gluePathUnit = gluePathGroup.listGlueUnit[i];
                if (gluePathUnit.listContinueItems.Count > 0)
                {
                    double dMaxXItem, dMaxYItem, dMinXItem, dMinYItem;
                    foreach (GluePathItemBase item in gluePathUnit.listContinueItems)
                    {
                        item.GetItemMinMaxXY(out dMinXItem, out dMinYItem, out dMaxXItem, out dMaxYItem);
                        if (dMinXItem < dMinX)
                        {
                            dMinX = dMinXItem;
                        }
                        if (dMinYItem < dMinY)
                        {
                            dMinY = dMinYItem;
                        }
                        if (dMaxXItem > dMaxX)
                        {
                            dMaxX = dMaxXItem;
                        }
                        if (dMaxYItem > dMaxY)
                        {
                            dMaxY = dMaxYItem;
                        }
                    }
                    dMinX = dMinX - 5;
                    dMinY = dMinY - 5;
                    dMaxX = dMaxX + 5;
                    dMaxY = dMaxY + 5;
                    bNeedModify = true;

                }
            }
            if (bNeedModify == false)
                return;

            float resultPxiSize = 0.0f;
            if (ClientRectangle.Width > ClientRectangle.Height)
                resultPxiSize = ClientRectangle.Height;
            else
                resultPxiSize = ClientRectangle.Width;

            float fScanX = (float)((ClientRectangle.Width - 100) / (dMaxX - dMinX));
            float fScanY = (float)((ClientRectangle.Height - 100) / (dMaxY - dMinY));

            float fScanFit = 1;
            if (fScanX < fScanY)
                fScanFit = fScanX;
            else
                fScanFit = fScanY;

            if (fScanFit < 0.6)
                fScanFit = 0.6f;

            fScan = fScanFit;


            fTransX = -(float)((dMaxX + dMinX) / 2);
            fTransY = -(float)((dMaxY + dMinY) / 2);

            // 把原点移到左下角，并把矩形0，0，视野中心
            MatrixPart = new Matrix(1, 0, 0, -1, (ClientRectangle.Width / 2), (ClientRectangle.Height / 2));
            MatrixPart.Scale(fScan, fScan, MatrixOrder.Prepend);
            MatrixPart.Translate(fTransX, fTransY);

            MatrixPartInvert = new Matrix();
            MatrixPartInvert = MatrixPart.Clone();
            MatrixPartInvert.Invert();

            DrawPathGlueGroup();
        }
        private void toolStripButtonModify_Click(object sender, EventArgs e)
        {
            SetUnCheckAll();
            this.Cursor = Cursors.Cross;
            toolStripButtonModify.Checked = true;
        }

        private void toolStripButtonSPLine_Click(object sender, EventArgs e)
        {
            SetUnCheckAll();
            this.Cursor = Cursors.Cross;
            toolStripButtonSPLine.Checked = true;
            if (toolStripButtonContinue.Checked)
            {
                if (pathUnit.listContinueItems.Count > 0)
                {

                    if (toolStripComboBoxDrawType.SelectedIndex == 0)
                    {
                        splineCurrent = new GluePathItemSPLine();
                        double dLastPosX, dLastPosY, dLastPosZ, dLastPosU;
                        int iIndex = pathUnit.listContinueItems.Count - 1;
                        pathUnit.listContinueItems[iIndex].GetLastPoint(out dLastPosX, out dLastPosY, out dLastPosZ, out dLastPosU);
                        PointData newPoint = new PointData();
                        newPoint.X = dLastPosX;
                        newPoint.Y = dLastPosY;
                        splineCurrent.ListPoints.Add(newPoint);

                        PointData newPoint1 = new PointData();
                        newPoint1.X = dLastPosX;
                        newPoint1.Y = dLastPosY;
                        splineCurrent.ListPoints.Add(newPoint1);

                        splineCurrent.iDrawStep = 1;
                        splineCurrent.Select = true;
                    }
                    else
                    {
                        lineCurrent = null;
                    }
                    //}
                    //else
                    //{

                    //}
                }
                else
                {
                    splineCurrent = null;
                }
            }
            else
            {
                lineCurrent = null;
            }
        }
        private void DrawerControl_SizeChanged(object sender, EventArgs e)
        {
            
            fScan = 1;
            fTransX = 0;
            fTransY = 0;
            // 把原点移到左下角，并把矩形0，0，视野中心
            MatrixPart = new Matrix(1, 0, 0, -1, (ClientRectangle.Width / 2), (ClientRectangle.Height / 2));
            MatrixPart.Scale(fScan, fScan, MatrixOrder.Prepend);
            MatrixPart.Translate(fTransX, fTransY);

            MatrixPartInvert = new Matrix();
            MatrixPartInvert = MatrixPart.Clone();
            MatrixPartInvert.Invert();

            m_BmpDraw = new Bitmap(ClientRectangle.Width, ClientRectangle.Height, System.Drawing.Imaging.PixelFormat.Format24bppRgb);
            m_bmpGraphic = Graphics.FromImage(m_BmpDraw);
            m_bmpGraphic.SmoothingMode = SmoothingMode.AntiAlias;

            SetToAutoFill();
        }
        public void DrawPoint(Pen pen, double dPosX, double dPosY)
        {
            PointF[] PtTemp = new System.Drawing.PointF[4];
            float fSize = 5 / fScan;
            PtTemp[0].X = (float)dPosX - fSize;
            PtTemp[0].Y = (float)dPosY + fSize;
            PtTemp[1].X = (float)dPosX + fSize;
            PtTemp[1].Y = (float)dPosY + fSize;
            PtTemp[2].X = (float)dPosX + fSize;
            PtTemp[2].Y = (float)dPosY - fSize;
            PtTemp[3].X = (float)dPosX - fSize;
            PtTemp[3].Y = (float)dPosY - fSize;
            MatrixPart.TransformPoints(PtTemp);
            m_bmpGraphic.FillRectangle(new SolidBrush(pen.Color), PtTemp[0].X, PtTemp[0].Y, PtTemp[2].X - PtTemp[0].X, PtTemp[2].Y - PtTemp[0].Y);
        }
        public void DrawPoint(Pen pen, double dPosX, double dPosY, double dSize)
        {
            PointF[] PtTemp = new System.Drawing.PointF[4];
            float fSize = 5 / fScan;
            PtTemp[0].X = (float)dPosX- fSize;
            PtTemp[0].Y = (float)dPosY+ fSize;
            PtTemp[1].X = (float)dPosX + fSize;
            PtTemp[1].Y = (float)dPosY + fSize;
            PtTemp[2].X = (float)dPosX + fSize;
            PtTemp[2].Y = (float)dPosY - fSize;
            PtTemp[3].X = (float)dPosX - fSize;
            PtTemp[3].Y = (float)dPosY - fSize;
            MatrixPart.TransformPoints(PtTemp);
            m_bmpGraphic.DrawRectangle(pen, PtTemp[0].X, PtTemp[0].Y, PtTemp[2].X- PtTemp[0].X, PtTemp[2].Y - PtTemp[0].Y);
        }
        public bool GetCurrentUnitHaveSelectItem()
        {
            if (pathUnit == null)
                return false;
            foreach(GluePathItemBase item in pathUnit.listContinueItems)
            {
                if (item.Select)
                    return true;
            }
            return false;
        }

        private void toolStripComboBoxDrawType_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (toolStripComboBoxDrawType.SelectedIndex == 1)
            {
                if (GetCurrentUnitHaveSelectItem() == false)
                {
                    MessageBox.Show("没有选择对象");
                    toolStripComboBoxDrawType.SelectedIndex = 0;
                }
            }
        }

        private void toolStripComboBoxItems_DropDown(object sender, EventArgs e)
        {
            toolStripComboBoxItems.Items.Clear();
            if (pathUnit != null)
            {
                for (int i=0;i< pathUnit.listContinueItems.Count;i++)
                {
                    toolStripComboBoxItems.Items.Add("Item" + (i + 1));
                }
            }
        }

        private void toolStripComboBoxItems_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (pathUnit != null)
            {
                for (int i = 0; i < pathUnit.listContinueItems.Count; i++)
                {
                    pathUnit.listContinueItems[i].Select = false; 
                }
                pathUnit.listContinueItems[toolStripComboBoxItems.SelectedIndex].Select = true;
            }
            foreach (GluePathItemBase item in pathUnit.listContinueItems)
            {
                bool bCurrentSelect = item.Select;
                if (propertyGrid != null)
                {
                    if (bCurrentSelect == true)
                    {
                        propertyGrid.SelectedObject = item;
                        propertyGrid.Refresh();

                    }
                }
            }
            DrawPathGlueGroup();
        }

        private void toolStripButtonDelete_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("是否真的删除选择项目", "是否真的删除选择项目", MessageBoxButtons.YesNo) != DialogResult.Yes)
                return;
            if (pathUnit != null)
            {
                for (int i = pathUnit.listContinueItems.Count-1; i > -1; i--)
                {
                    if (pathUnit.listContinueItems[i].Select)
                    {
                        pathUnit.listContinueItems.RemoveAt(i);
                    }
                }
            }
            DrawPathGlueGroup();
        }

        private void DrawerControl_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.Control)
            {
                if (e.KeyCode == Keys.A)
                {
                    if (pathUnit != null)
                    {
                        for (int i = 0; i < pathUnit.listContinueItems.Count; i++)
                        {
                            pathUnit.listContinueItems[i].Select = true;
                        }
                        DrawPathGlueGroup();
                    }
                }
            }
        }

        public static Bitmap GetWorldMapImage(out double dStartPosX, out double dStartPosY, out double dEndX, out double dEndY, double dMotorPosX = 0.0, double dMotorPosY = 0.0)
        {

            dStartPosX = 0;
            dStartPosY = 0;
            dEndX = 0;
            dEndY = 0;
            int iImageWidth, iImageHeight;
            HTuple dResultX = 0.0;
            HTuple dResultY = 0.0;
            double dScale = 0.01;
            HTuple dResultX1 = 0.0;
            HTuple dResultY1 = 0.0;
            HTuple dResultX2 = 0.0;
            HTuple dResultY2 = 0.0;

            iImageWidth = ImageCalibModule.ImageCalibManager.ImageCalibHandeye.CalibPara.StartImageWidth;
            iImageHeight = ImageCalibModule.ImageCalibManager.ImageCalibHandeye.CalibPara.StartImageHeight;
          

            #region GetHImage
            

            ImageCalibParameter calibParameter= ImageCalibModule.ImageCalibManager.ImageCalibHandeye.CalibPara;
            

            HTuple cameraParam = new HTuple();
            HTuple finalPoseTemp = new HTuple();
            HTuple finalPose = new HTuple();
            HTuple PoseNewOrigin = new HTuple();

            HImage imageCur= ImageTaskModule.ImageTaskManager.ImageTask("ProductDetect").GetRegeditFile();
            if (imageCur == null)
                return null;

            cameraParam.Append(calibParameter.ResultFocus);
            cameraParam.Append(calibParameter.ResultKappa);
            cameraParam.Append(calibParameter.ResultSx);
            cameraParam.Append(calibParameter.ResultSy);
            cameraParam.Append(calibParameter.ResultCx);
            cameraParam.Append(calibParameter.ResultCy);
            cameraParam.Append(calibParameter.ResultImageWidth);
            cameraParam.Append(calibParameter.ResultImageHeight);

            finalPoseTemp.Append(calibParameter.FianlPosX);
            finalPoseTemp.Append(calibParameter.FianlPosY);
            finalPoseTemp.Append(calibParameter.FianlPosZ);
            finalPoseTemp.Append(calibParameter.FianlPosU);
            finalPoseTemp.Append(calibParameter.FianlPosV);
            finalPoseTemp.Append(calibParameter.FianlPosW);
            finalPoseTemp.Append(0);
            double ForceOffZ = calibParameter.FianlPosZ * ImageCalibModule.ImageCalibManager.ImageCalibHandeye.ZOffSet / ImageCalibModule.ImageCalibManager.ImageCalibHandeye.FixHeight;
            HOperatorSet.SetOriginPose(finalPoseTemp, 0, 0, ForceOffZ, out finalPose);

            HOperatorSet.ImagePointsToWorldPlane(cameraParam, finalPose, iImageHeight / 2 - 100, iImageWidth / 2 - 100, 1, out dResultX1, out dResultY1);
            HOperatorSet.ImagePointsToWorldPlane(cameraParam, finalPose, iImageHeight / 2 + 100, iImageWidth / 2 + 100, 1, out dResultX2, out dResultY2);
            double dDistance1 = Math.Sqrt(200 * 200 + 200 * 200);
            double dDistance2 = Math.Sqrt((dResultX2 - dResultX1) * (dResultX2 - dResultX1) + (dResultY2 - dResultY1) * (dResultY2 - dResultY1));
            dScale = dDistance2 / dDistance1;
            HOperatorSet.ImagePointsToWorldPlane(cameraParam, finalPose, iImageHeight / 2, iImageWidth / 2, 1, out dResultX, out dResultY);
            HOperatorSet.SetOriginPose(finalPose, dResultX - dScale * iImageWidth / 2.0, dResultY - dScale * iImageHeight / 2.0, 0, out PoseNewOrigin);
            HObject hImageOut, hImageMap;
            HOperatorSet.GenImageToWorldPlaneMap(out hImageOut, cameraParam, PoseNewOrigin, iImageWidth, iImageHeight, iImageWidth, iImageHeight, dScale, "bilinear");
            HOperatorSet.MapImage(imageCur, hImageOut, out hImageMap);


            HImage hResultImage = new HImage(hImageMap);
            HImage hResultImage1 = new HImage();
            hResultImage1 = hResultImage.MirrorImage("row");
            //hResultImage1 = hResultImage.CopyImage();
            hResultImage1.WriteImage("bmp", 0,"ABC.bmp");
            #endregion
            hResultImage1.Dispose();




            System.Drawing.Bitmap bitmap = new Bitmap("ABC.bmp");
            
            System.Drawing.Bitmap bitmapResult = (System.Drawing.Bitmap)bitmap.Clone();

            double dImageCenterWorldX, dImageCenterWorldY;
            ImageCalibModule.ImageCalibManager.ImageCalibHandeye.TramImageToWorld(iImageWidth / 2, iImageHeight / 2, dMotorPosX, dMotorPosY, out dImageCenterWorldX, out dImageCenterWorldY);

            dStartPosX = dImageCenterWorldX - iImageWidth * dScale / 2;
            dStartPosY = dImageCenterWorldY + iImageHeight * dScale / 2;
            dEndX = dImageCenterWorldX + iImageWidth * dScale / 2;
            dEndY = dImageCenterWorldY - iImageHeight * dScale / 2;
            bitmap.Dispose();
            return bitmapResult;
            //return hResultImage1;

        }
    }
}
